package com.mfnote.view.hand;

import com.mfnote.util.StringUtils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

/**
 * 重写EditText支持插入图片
 * 
 * @author DC
 * 
 */
public class NewEditText extends EditText {
	private HandWritingActivity handActivity = null;

	public NewEditText(Context context) {
		super(context);
		handActivity = (HandWritingActivity) context;
		setFocusable(true);
		setFocusableInTouchMode(true);
		setEnabled(true);
		
	}

	public NewEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	/** 在editText中插入图片 **/
	public void insertBitmap(Bitmap bitmap) {
		// 将bitmap转化成drawable对象
		BitmapDrawable drawable = new BitmapDrawable(bitmap);
		SpannableString sp = new SpannableString("edit");
		drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
				drawable.getIntrinsicHeight());
		ImageSpan ispan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
		sp.setSpan(ispan, 0, "edit".length(),
				Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

		append(sp);
		// TODO 每次插入图片之后插入一个空字符
		append(" ");
		// append(text, start, end);

		Log.d("insert", "===insert===");
	}

	/** 插入空格 **/
	public void insertBlank() {
		this.append("\t");
		CharSequence textBlank = this.getText();
		if (textBlank instanceof Spannable) {
			Spannable ap = (Spannable) textBlank;
			Selection.setSelection(ap, textBlank.length());
		}
	}

	/** 插入换行 **/
	public void insertNextLine() {
		this.append("\n\r");
		CharSequence text = this.getText();
		if (text instanceof Spannable) {
			Spannable ap = (Spannable) text;
			Selection.setSelection(ap, text.length());
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		requestFocus();
		int action = event.getAction();
		Layout layout = getLayout();
		int line = 0;
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			handActivity.newwriting_ll_view.setVisibility(View.VISIBLE);
			handActivity.showHandView = true;

			// 获取当前点的点的索引
			line = layout.getLineForVertical(getScrollY() + (int) event.getY());
			int index = layout.getOffsetForHorizontal(line, event.getX());

			// 设置光标的位置,判断光标的后面如果有一个(" ")则将光标位置后移一位
			int length = getText().toString().length();
			String text = getText().toString();
			Log.d("msg", length+"--"+text+"--");
            
			//设置光标到图片后的空格后面
			if (index<length) {
				String str = text.substring(index, index + 1);
				Log.d("str", "--" + str + "--");
				if (" ".equals(str)) {
					setSelection(index + 1);
					index++;
					Log.d("dc", "dc");
				} else {
					setSelection(index);
				}
			}else if(index==length){
				setSelection(index);
			}
            
			
			String str = text.substring(0, index);
			Log.d("str", "--"+str+"--");
			//获取光标位置之前插入了几次数据
			int cursorindex = StringUtils.getIndex(str);

			handActivity.currentCursorIndex = cursorindex;
			handActivity.prepLength = str.length();
			Log.d("hand", "=====index=====" + index + "--"
					+ text.subSequence(0, index).toString());
			break;
		}
		return true;
	}
}
